package com.news.web.servlet.news;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.News;
import com.news.entity.Review;
import com.news.service.NewsService;
import com.news.service.ReviewService;
import com.news.service.impl.NewsServiceImpl;
import com.news.service.impl.ReviewServiceImpl;

/**
 * Servlet implementation class NewsSingleSerblet
 */
@WebServlet("/NewsSingleServlet")
public class NewsSingleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 设置编码方式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		System.out.println("进入singleservlet");
		// 创建NewsService实例
		NewsService newsService = new NewsServiceImpl();
		// 创建ReviewService实例
		ReviewService reviewService = new ReviewServiceImpl();
		// 获取传入参数
		String newsIdStr = request.getParameter("newsId");
		System.out.println(newsIdStr+"这是获取");
		 //判断是否有传值
		if (newsIdStr == null || "".equals(newsIdStr)) {
			// 将数据存入
			request.setAttribute("categoryMap", null);
			// 跳转回原页面
			request.getRequestDispatcher("index.jsp").forward(request, response);
			return;
		}
		
		int newsId=1;
//		//判断传入的是不是数字
		try {
			newsId= Integer.parseInt(newsIdStr);
		} catch (Exception e) {
			// 将数据存入
			request.setAttribute("categoryMap", null);
			// 跳转回原页面
			request.getRequestDispatcher("index.jsp").forward(request, response);
			return;
		}
		// 创建map
		Map<String, Object> singleMap = new HashMap<String, Object>();
		// 查询对应新闻
		News news = newsService.queryNews(newsId);
		// 将数据存入
		request.setAttribute("newsType", news.getNewsType());
		//添加新闻热度
		news.setNewsHeat(news.getNewsHeat() + 1);
		// 修改新闻
		newsService.updateNews(news);
		// 数据写入map
		singleMap.put("news", news);
		// 查询新闻对应的评论
		List<Review> reviewList = reviewService.queryReviewInNews(newsId);
		// 数据写入map
		singleMap.put("reviewList", reviewList);
		// 查询相关新闻,传入类型和需要的条数
		List<News> relatedNewsList = newsService.queryNewsTypeNum(news.getNewsType(), 4);
		// 数据写入map
		singleMap.put("relatedNewsList", relatedNewsList);
		//查询右侧栏热度最高的四条新闻
		List<News> queryRightNewsHeatNumList = newsService.queryNewsHeatNum(4);
		// 装入数据
		singleMap.put("queryRightNewsHeatNumList", queryRightNewsHeatNumList);
		// 查询右侧栏评论最高的四条新闻
		List<News> queryNewsReviewNum = newsService.queryNewsReviewNum(4);
		// 装入数据
		singleMap.put("queryNewsReviewNum", queryNewsReviewNum);
		// 将数据存入
		request.setAttribute("singleMap", singleMap);
		System.err.println("错误1");
		// 跳转回原页面
		request.getRequestDispatcher("single.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
